package xyz.icxl.android.architecture.observer

import android.view.View
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.SnackbarUtils
import xyz.icxl.android.architecture.ui.mvvm.BaseViewModel

/**
 * 页面Snackbar状态观察者
 */
class PageSnackBarStatusObserver(private val view: View) : Observer<BaseViewModel.PageSnackBarStatus> {
    override fun onChanged(t: BaseViewModel.PageSnackBarStatus?) {
        if (t?.show == true
            || t?.showError == true
            || t?.showSuccess == true
            || t?.showWarning == true
        ) {
            val snackbar = SnackbarUtils
                .with(view)
                .setMessage(t.message)
                .setDuration(t.duration)
            if (t.show)
                snackbar.show(t.isShowTop)
            if (t.showError)
                snackbar.showError(t.isShowTop)
            if (t.showSuccess)
                snackbar.showSuccess(t.isShowTop)
            if (t.showWarning)
                snackbar.showWarning(t.isShowTop)
        }
        if (t?.dismiss == true) {
            SnackbarUtils.dismiss()
        }
    }

    fun dismiss() {
        SnackbarUtils.dismiss()
    }
}